/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import condivisa.Persona;
import connezione.BeanPool;
import connezione.PooledConnection;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author sestari
 */
public class BeanUser extends Persona {

    private PooledConnection con = null;

    /** Creates a new instance of login */
    public BeanUser() {
    }

    public void database() {


        BeanPool myBeanpool;
        myBeanpool = (BeanPool) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("pool");
        con = myBeanpool.getPool().checkOut();

    }

    public String falogin() {

        if (con == null) {
            database();
        }
        Statement st = null;
        try {
            st = (Statement) con.createStatement();
                   String command = "Select * from Persona where username =\"" + getUsername() + "\" and password=\"" + getPassword() + "\"";             
	            //String command = "Select * from Persona";		
	            ResultSet rs = (ResultSet) st.executeQuery(command);	                    
	            if (rs.next()) {               
                        
                        setId(rs.getInt("id"));	
                        setId_medico(rs.getInt("id_medico"));
	                setNome(rs.getString("nome"));		
	                setCognome(rs.getString("cognome"));		
	                setData_nascita(rs.getDate("data_nascita"));		
	                setSesso(rs.getString("sesso").charAt(0));		
	                setLuogo_nascita(rs.getString("luogo_nascita"));		
	                setIndirizzo(rs.getString("indirizzo"));		
	                setCap(rs.getInt("cap"));		
	                setCooX(rs.getLong("latitudine"));		
	                setCooY(rs.getLong("longitudine"));		
	                setId_padre(rs.getInt("id_padre"));		
	                setId_madre(rs.getInt("id_madre"));		
	                setEmail(rs.getString("e_mail"));		
	                setCelullare(rs.getString("cellulare"));		
	                setId_capo_vilaggio(rs.getInt("id_capo_villaggio"));		
	                setFoto(rs.getString("foto"));		
	                setTipo_persona(tipo_persona.valueOf(rs.getString("tipo_persona")));




                    switch (getTipo_persona()) {
                        case PAZIENTE: {
                            return "paziente";
                        }
                        case MEDICO: {
                            return "medico";
                        }

                        case OPERATORE_USL: {
                            return "usl";
                        }

                        default:
                            return "login";
                    }

                

            } else {
                FacesMessage msg = lingua.BeanLingua.getMessage("message.message", "errorLogin", null);
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return "loginError";

            }
        } catch (SQLException ex) {
            Logger.getLogger(BeanUser.class.getName()).log(Level.SEVERE, null, ex);
        }


        return "login";



    }

    public PooledConnection getCon() {
        return con;
    }

    public void logoff() {
        try {
            con.close();
            con = null;

            FacesContext fc = FacesContext.getCurrentInstance();
            HttpSession s = (HttpSession) fc.getExternalContext().getSession(false);        
            fc.getExternalContext().redirect("/xpto/");
            s.invalidate();

        } catch (SQLException ex) {
            Logger.getLogger(BeanUser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(BeanUser.class.getName()).log(Level.SEVERE, null, ex);
        }




    }
}
